package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**权限服务实例
 * @author YH
 * @date 2020/4/17 21:02
 */
@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    //查询所有权限列表
    @Override
    public List<Map<String, Object>> findPermissionByAll() {
        return permissionDao.findPermissionByAll();
    }


    public PageResult findAll(QueryPageBean queryPageBean) {
        Integer offset = (queryPageBean.getCurrentPage()-1)*queryPageBean.getPageSize();
        Integer limit = queryPageBean.getPageSize();
        List<Map<String, Object>> mapList = permissionDao.findAll(queryPageBean.getQueryString(),limit,offset);
        List<Map<String, Object>> mapListCount = permissionDao.findAll(queryPageBean.getQueryString(),null,null);
        PageResult pageResult = new PageResult(Long.valueOf(mapListCount.size()), mapList);
        return pageResult;
    }

    /**
     * 新增权限
     * 往权限表写记录
     * @param permission
     */
    @Override
    public void add(Permission permission) {

        //往权限表写记录
        //PermissionDao.setRoleAndPermissionIds(role.getId(),permissionIds);
        permissionDao.add(permission);
    }



    /**
     * 根据角色id查询权限
     * @param roleId
     * @return
     */
    @Override
    public Set<Permission> findById(Integer roleId) {
        return permissionDao.findPermissionsByRoleId(roleId);
    }

    @Override
    public Map QueryById(Integer Id) {
        return permissionDao.findPermissionsById(Id);
    }


    /**
     * 编辑权限
     * @param permission
     */
    @Override
    public void edit(Permission permission) {
        //更新权限表 update 语句
        permissionDao.edit(permission);

    }


    /**
     * 根据角色id删除权限
     * @param roleId
     * @param permissionIds
     */
    @Override
    public void deleteById(Integer roleId, Integer[] permissionIds) {
        permissionDao.deleteById(roleId,permissionIds);
    }

    /**
     * 删除单个权限
     * @param permissionId
     */
    @Override
    public void delete(Integer permissionId) {
        //查询角色和权限表关系是否存在，存在则无法删除
        int count = permissionDao.findPermissionsByRoleIdO(permissionId);
        if(count>0){
            throw new RuntimeException(MessageConstant.DELETE_PERMISSION_FAIL);//一定要install
        }

        //关系不存在，则可以删除
        permissionDao.delete(permissionId);
        return ;
    }


    /*
    往角色和权限中间表写记录(新增、编辑检查组公共的代码)
     */

    public void setRoleAndPermissionIds(Integer roleId,Integer[] permissionIds){
        if(permissionIds != null && permissionIds.length> 0){
            for (Integer permissionId : permissionIds) {
                //为了方便测试传入map对象
                Map<String,Integer> map = new HashMap<>();
                map.put("roleId",roleId);
                map.put("permissionId",permissionId);
                permissionDao.setRoleAndPermissionIds(map);
            }
        }
    }
}
